package com.asset.salesforce.validator;


import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.Call;
import com.asset.salesforce.form.CallForm;
import com.asset.salesforce.service.CallService;

public class CallNotExistConstraintValidator implements ConstraintValidator<CallNotExit	, Object> {

	@Autowired
	private CallService callService;
	
	public void initialize(CallNotExit arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean isValid(Object arg0, ConstraintValidatorContext constraintValidatorContext) {
		boolean success = false; 
		CallForm callForm = (CallForm)arg0;
		List<Call>calls = callService.getCallEventsByEmployeeId(callForm.getEmpId(),callForm.getParentId());
		System.out.println("sddddddddddddddddddddddddddddddddddddddd"+calls.size());
		String targetStartTime = callForm.getStartTime();
		String targetEndTime = callForm.getEndTime();
		if(targetStartTime!=null&&!targetStartTime.equalsIgnoreCase("")||targetEndTime!=null&&!targetStartTime.equalsIgnoreCase("")){
			if(targetStartTime!=null&&!(targetStartTime.equalsIgnoreCase(""))){
             	if(targetEndTime!=null&&!(targetEndTime.equalsIgnoreCase(""))){
			//success = true;
		String[] TargetStartTimeForTime = targetStartTime.split(":");
		Integer targetStartTime1 = (Integer.parseInt(TargetStartTimeForTime[0])*60)+Integer.parseInt(TargetStartTimeForTime[1]);		
		
		
		String[] targetEndTimeForTime = targetEndTime.split(":");
		Integer targetEndTime1 = (Integer.parseInt(targetEndTimeForTime[0])*60)+Integer.parseInt(targetEndTimeForTime[1]);
		
		//get the date
		String dateValue = callForm.getDate();
		String[] TargetDate = dateValue.split("/");
		for (int i = 0; i < TargetDate.length; i++) {
			System.out.println(TargetDate[i]);
		}
		if(calls.size()==0){
			success = true;
		}
		else{
		for (Call call : calls) {
			String sourceStartTime = call.getStartTime();
			System.out.println("fdsssssssssssss"+sourceStartTime);
			String []sourceStartTimeFromDatabase = sourceStartTime.split(":");
			Integer sourceStartTime1 = -1;
			if(sourceStartTimeFromDatabase.length==2&&isNumeric(sourceStartTimeFromDatabase[0])&&isNumeric(sourceStartTimeFromDatabase[1])){
				sourceStartTime1 = (Integer.parseInt(sourceStartTimeFromDatabase[0])*60)+Integer.parseInt(sourceStartTimeFromDatabase[1]);
			}
			
			String sourceEndTime = call.getEndTime();
			String []sourceEndTimeFromDatabase = sourceEndTime.split(":");
			Integer sourceEndTime1 = -1;
			if(sourceEndTimeFromDatabase.length==2&&isNumeric(sourceEndTimeFromDatabase[0])&&isNumeric(sourceEndTimeFromDatabase[1])){
				sourceEndTime1= (Integer.parseInt(sourceEndTimeFromDatabase[0])*60)+Integer.parseInt(sourceEndTimeFromDatabase[1]);
			}
			//get the date
			String sdateValue = call.getDate().toString().substring(0, 10);
			String[] sourceTargetDate = sdateValue.split("-");
			for (int i = 0; i < sourceTargetDate.length; i++) {
				System.out.println(sourceTargetDate[i]);
			}
			String dateFinal = sourceTargetDate[2]+"/" +sourceTargetDate[1]+"/" +sourceTargetDate[0];
			System.out.println(dateFinal);
			if(!dateValue.equals(dateFinal)){
				 success = true;
	}
		else{
			
			if(sourceStartTime1 == -1 || sourceEndTime1 == -1 ){
				success = true;
				}else{
					if(targetStartTime1>=sourceEndTime1 && targetEndTime1>targetStartTime1
							||targetEndTime1<=sourceStartTime1&&targetStartTime1<targetEndTime1){
						 success = true;
					}
					else{
						 success=false;
					}
				}
			}
		
	}
		}
		return success;
             	}
             	else{success=true;}
        	 }
        else{
        	success=true;
        }
		}
		else{
//			constraintValidatorContext.buildConstraintViolationWithTemplate("Please,Enter the start time").addNode("startTime").addConstraintViolation();
//			constraintValidatorContext.buildConstraintViolationWithTemplate("Please,Enter the End time").addNode("endTime").addConstraintViolation();
//			constraintValidatorContext.disableDefaultConstraintViolation();
//			return success;
			success=true;
		}
		return success;
	}
	public  boolean isNumeric(String str){
	    for (char c : str.toCharArray())
	    {
	        if (!Character.isDigit(c)) return false;
	    }
	    return true;
	}
}
